<?php
/**
 * Clears cache of products module
 * 
 * @author Konstantin Shamko <konstantin.shamko@gmail.com> 
 * @version 0.0.1
 * @copyright  Copyright (c) 2009 Konstantin Shamko
 * @package Products Module
 * @license  New BSD License
 *
 */
class Helpers_Products_Cache{
	
	/**
	 * Clears product cache by product ID
	 *
	 * @param integer $product_id
	 */
	public static function clearProductCache($product_id){
		$cache = Zend_Registry::get('cache');
		$cache->remove('product'.$product_id);
	}

	/**
	 * Clears products cache by template
	 *
	 * @param integer $template_id
	 */
	public static function cleanProductCacheByTemplate($template_id){
		$cache = Zend_Registry::get('cache');
		$oProducts = new Models_Products();
		foreach ($oProducts->fetchAll('product_template_id=\''.$template_id.'\'') as $product){
			$cache->remove('product'.$product->product_id);	
		}
	}
	
	/**
	 * Clears products cache by attribute.
	 *
	 * @param integer $attribute_id
	 */
	public static function cleanProductCacheByAttribute($attribute_id){
		$oAttr = new Models_ProductAttributes();
		$attr = $oAttr->find($attribute_id)->current();
		
		if($attr){
			$templates = $attr->findManyToManyRowset('Models_ProductTemplates','Models_ProductTemplates_Attributes');
						
			foreach ($templates as $template){
				$_t[$template->product_template_id] = $template->product_template_id;				
			}
			foreach($_t as $id){
				self::cleanProductCacheByTemplate($id);
			}
		}
	}	
}